/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cn.ekuma.epos.datalogic.define.dao;

import java.util.List;

import cn.ekuma.data.dao.BaseDAO;
import cn.ekuma.epos.db.table.I_Payment;

import com.openbravo.bean.PaymentInfo;
import com.openbravo.bean.sales.Ticket;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.DataParams;
import com.openbravo.data.loader.DataRead;
import com.openbravo.data.loader.DataWrite;
import com.openbravo.data.loader.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.PreparedSentence;
import com.openbravo.data.loader.SerializerWriteParams;
import com.openbravo.data.loader.TableDefinition;
import com.openbravo.data.model.Field;
import com.openbravo.format.Formats;
import com.openbravo.pos.bean.PaymentInfoTicket;

/**
 *
 * @author Administrator
 */
public class PaymentInfoTicketDAO extends BaseDAO<PaymentInfoTicket>{

    public PaymentInfoTicketDAO(I_Session s) {
        super(s);
    }

    @Override
    public TableDefinition getTable() {
        return new TableDefinition( s,
                "PAYMENTS",
                new Field[]{
        		new Field(I_Payment.ID,Datas.STRING,Formats.STRING),
                new Field(I_Payment.RECEIPT,Datas.STRING,Formats.STRING,Ticket.class),
                new Field(I_Payment.PAYMENT,Datas.STRING,Formats.STRING),
                new Field(I_Payment.TOTAL,Datas.DOUBLE,Formats.DOUBLE),
                new Field(I_Payment.TRANSID,Datas.STRING,Formats.STRING)
                },
                new int[]{0});
    }

    @Override
    public void writeInsertValues(DataWrite dp, PaymentInfoTicket obj) throws BasicException {
    	dp.setString(1,obj.getId());
        dp.setString(2,obj.getReceipt());
        dp.setString(3,obj.getM_sName());
        dp.setDouble(4,obj.getTotal());
        dp.setString(5,obj.getM_transactionID());
    }


    public PaymentInfoTicket readValues(DataRead dr,PaymentInfoTicket p) throws BasicException {
        if(p==null)
        	p=new PaymentInfoTicket();
        p.setId(dr.getString(1));
        p.setReceipt(dr.getString(2));
        p.setM_sName(dr.getString(3));
        p.setM_dTicket(dr.getDouble(4));
        p.setM_transactionID(dr.getString(5));
        return p;
    }

    @Override
    public Class getSuportClass() {
        return PaymentInfoTicket.class;
    }
    
    public List<PaymentInfo> listByReceipt(final String receiptId)throws BasicException {
    	return new PreparedSentence(s, this.getTableDefinition().getListSQL()+" WHERE "+I_Payment.RECEIPT+" = ? ", SerializerWriteParams.INSTANCE, this).list(new DataParams(){
            public void writeValues() throws BasicException {
                setString(1, receiptId);
            }
        });
    }

}
